package com.gx.smart.building.ui.message.viewmodel

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.gx.smart.base.BaseViewModel
import com.gx.smart.lib.http.api.RetrofitManager
//import com.gx.smart.building.MessageRecordApiService
import com.gx.smart.lib.http.api.config.ApiConfig
import com.gx.smart.lib.http.api.response.Message
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 *@author: xiaosy
 *时间: 2022/4/7
 *@Description: java类作用描述
 *@Version: 1.0
 **/
class MessageViewModel(app: Application) : BaseViewModel(app) {
    private val list: MutableLiveData<List<Message>> = MutableLiveData()
    fun messageRecordQuery(currentPage: Int): LiveData<List<Message>> {
        val call= RetrofitManager.getInstance().messageRecordApiService.messageRecordQuery()
        call?.enqueue(object: Callback<com.gx.smart.lib.http.api.response.MessageRecordQueryResponse?> {
            override fun onResponse(
                call: Call<com.gx.smart.lib.http.api.response.MessageRecordQueryResponse?>,
                response: Response<com.gx.smart.lib.http.api.response.MessageRecordQueryResponse?>
            ) {
                val result = response.body()
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    list.value = result.content
                } else {
                    ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
                }
            }

            override fun onFailure(
                call: Call<com.gx.smart.lib.http.api.response.MessageRecordQueryResponse?>,
                t: Throwable
            ) {
                ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
            }

        })
        return list
    }

    //设置已读未读
    fun messageRecordRead(messageId:String) {

        val call = RetrofitManager.getInstance().messageRecordApiService.messageRecordRead()
        call?.enqueue(object:Callback<com.gx.smart.lib.http.api.response.CommonResponse?> {
            override fun onResponse(
                call: Call<com.gx.smart.lib.http.api.response.CommonResponse?>,
                response: Response<com.gx.smart.lib.http.api.response.CommonResponse?>
            ) {
                val result = response.body()
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                if (result.comResp.code == 100) {
                } else {
                    ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
                }
            }

            override fun onFailure(
                call: Call<com.gx.smart.lib.http.api.response.CommonResponse?>,
                t: Throwable
            ) {
                ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
            }

        })
    }

}